﻿using Functionality;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FunctionalityTest
{
    
    
    /// <summary>
    ///This is a test class for DoorPrizeEventTest and is intended
    ///to contain all DoorPrizeEventTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DoorPrizeEventTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        protected DoorPrizeEvent GetMiniEvent()
        {
            DoorPrizeEvent target = new DoorPrizeEvent(1, 1);
            target.Participants.Add(new Person("Sam Smith"));
            target.Prizes.Add(new Prize("Apple"));
            return target;
        }

        protected DoorPrizeEvent_Accessor GetMiniEvent_Accessor()
        {
            DoorPrizeEvent_Accessor target = new DoorPrizeEvent_Accessor(1, 1);
            target.Participants.Add(new Person("Sam Smith"));
            target.Prizes.Add(new Prize("Apple"));
            return target;
        }

        /// <summary>
        ///A test for DrawPrize
        ///</summary>
        [TestMethod()]
        public void DrawPrizeTest()
        {
            DoorPrizeEvent target = GetMiniEvent();

            target.DrawPrize();

            Assert.IsTrue(target.Participants[0].PrizesWon.Count != 0, "Prize not assigned properly to person");
            Assert.IsTrue(target.PrizeDrawings.Count != 0, "Drawing not stored properly");
        }

        /// <summary>
        ///A test for SelectPrize
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Functionality.dll")]
        public void SelectPrizeTest()
        {
            DoorPrizeEvent_Accessor target = GetMiniEvent_Accessor();

            Prize expected = target.Prizes[0];
            Prize actual;
            actual = target.SelectNextPrize();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for SelectPerson
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Functionality.dll")]
        public void SelectPersonTest()
        {
            DoorPrizeEvent_Accessor target = GetMiniEvent_Accessor();
            Person expected = target.Participants[0];
            Person actual;
            actual = target.SelectPerson();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for SelectPrize
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Functionality.dll")]
        [ExpectedException(typeof(Exception), "Out of prizes to select from")]
        public void SelectPrizeErrorCaseTest()
        {
            DoorPrizeEvent_Accessor target = new DoorPrizeEvent_Accessor(); // No prizes defined
            Prize actual;
            actual = target.SelectNextPrize();
        }

        /// <summary>
        ///A test for SelectPerson
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Functionality.dll")]
        [ExpectedException(typeof(Exception), "No valid options to select a person from")]
        public void SelectPersonNonTrivialTest()
        {
            DoorPrizeEvent_Accessor target = GetMiniEvent_Accessor();
            target.Participants.Add(new Person("Angela Apple"));
            target.Participants.Add(new Person("Bob Barker"));
            target.Participants.Add(new Person("Chris Cloud"));

            // Four people now, have a few drawings
            List<Person> peopleSelected = new List<Person>();
            for (int i = 0; i < 4; i++)
            {
                Person selected = target.SelectPerson();
                peopleSelected.Add(selected);
                selected.PrizesWon.Add(new Prize(String.Format("Prize{0}", i)));
            }

            // Verify we didn't pick anybody more than once.
            CollectionAssert.AllItemsAreUnique(peopleSelected, "\r\n\tpeopleSelected: {0}", String.Join(", ", peopleSelected.Select(person => person.FullName)));

            // Try to pick the next person. Since there is nobody left, we should throw
            // an exception.
            target.SelectPerson();
        }
    }
}
